﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SnelsteRoute
{
    public class Route
    {
        public Int32 NrOfHops { get { return Hops.Count; } }

        private List<Aspect> _hops = new List<Aspect>();
        public List<Aspect> Hops { get {return _hops; } private set { _hops = value;} }
        public Boolean Invalid { get; set; }

        public Aspect LastAspect
        {
            get{
                if (Hops == null || Hops.Count == 0)
                    return null;

                else 
                    return Hops[Hops.Count-1];
            }
        }

        private Int32 _cost = -1;
        public Int32 Cost
        {
            get
            {
                if (_cost == -1)
                    _cost = DetermineCost(this);

                return _cost;
            }
        }

        private int DetermineCost(Route route)
        {
            var totalCost = 0;

            foreach (var aspect in route.Hops)
            {
                totalCost += aspect.Cost;
            }
            return totalCost;
        }

        public void AddAspect(Aspect toAdd)
        {
            _hops.Add(toAdd);
        }

        public Route Clone()
        {
            return new Route(){ Hops = new List<Aspect>(_hops)};
        }

        public override string ToString()
        {
            return NrOfHops + ":" + String.Join("->",Hops.Select(a => a.Name).ToArray());
        }

        internal bool IsInvalid(int maxHops)
        {
            // todo, more interesting checks.
            return NrOfHops > maxHops;
        }

        internal bool IsComplete(Aspect start, Aspect finish, Int32 desiredNrOfHops)
        {
            if (Hops == null || Hops.Count == 0)
                return false;

            return Hops[0] == start && LastAspect == finish && NrOfHops == desiredNrOfHops;
        }

        
    }
}
